//  const BASE_URL = 'http://192.168.31.214:8082'
// const BASE_URL = 'https://zx.sweim.top'
const BASE_URL = 'https://mp.ywxktrip.com'
let token = ''
let request_list = []
let is_login = false;//是否登录
let request_num = 0; // 请求数
import { dataProcessing } from './dataProcessing'
const app = getApp()
/**
 * 获取访问token
 */
const login = cb => {
  // 记录所有请求
  request_list.push(cb)
  // 防止重复请求登录
  if (!is_login) {
    is_login = true;
  } else {
    return
  }
  // 登录请求
  wx.login({
    success: function (res) {
      request({
        url: `/api/login/${res.code}`,
      }).then(res => {
        if (res.data.code == 0) {
          getApp().globalData.sessionId = res.header.sessionid || res.header.sessionId;//webs用到
          token = res.header.sessionid || res.header.sessionId;
          // 全部请求回调
          request_list.forEach(cb => {
            cb && cb()
          })
          request_list = [];
          is_login = false;
        }
      })
    }
  })
}
/**
 * 数据请求封装
 */
const request = obj => {

  return new Promise((resolve, reject) => {
    wx.showNavigationBarLoading()//在标题栏中显示加载
    //带请求token
    if (token) {
      obj.data = obj.data || {}
    }
    request_num += 1
    obj.showOnload = (obj.hasOwnProperty('showOnload') ? obj.showOnload:true) // 有该属性的认该属性，没有的默认为true
    if (request_num == 1 && obj.showOnload) {
      wx.showLoading({ title: '加载中' })
    }
    wx.request({
      url: BASE_URL + obj.url,
      data: obj.data,
      header: {
        sessionid: token,
        'Content-Type':'application/x-www-form-urlencoded'
      },
      method: obj.method || 'POST',
      success: function (res) {
        request_num-=1
        if (request_num == 0 && obj.showOnload) {
          wx.hideLoading()
        }
        if (res.data.code == 501) {
          resolve(res.data)
        }else if (res.data.code == 502) {
          //用户身份过期
          login(() => {
            request(obj).then(res => {
              resolve(res)
            })
          })
        } else if (res.data.code == 500) {
          // 系统出错
        }else if (res.data.code == 504) {
          //查无信息类，需要在本页面提示信息，返回上一层页面
        } else  {
          // 成功回调
          // console.log('请求成功', res)
          resolve(is_login ? res : new dataProcessing(res.data));
        }
      },
      fail: function (err) {
        request_num-=1
        if (request_num == 0 && obj.showOnload) {
          wx.hideLoading()
        }
        reject(err)
      },
      complete: function (res) {
        wx.hideNavigationBarLoading()//完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
        let title = '';
        let comeBack = false;
        // console.log(obj,res)
        if(res.data){
          switch (Number(res.data.code)){
            case 0:
              break;
            case 500:
              title = '系统出错，请稍后重试';
              // comeBack = true;
              break;
            case 501:
              title = res.data.msg;
              break;
            case 504:
              title = '查无信息';
              comeBack = true;
              break;
            case 506:
            wx.redirectTo({url: '/pages/match_err/index'})
            break;
          }
        }
        if (title != '') {
          wx.showToast({ title: title, icon: 'none' })
        }
        if (comeBack) {
          setTimeout(() => {
            wx.navigateBack(1)
          }, 2000)
        }
      }
    })
  })
}

export default request